<template>
  <div class="ticket-container">
    <div class="header">
      <h1 class="title">我的车票</h1>
      <p class="subtitle">这里存储着当前用户购买的车票信息</p>
    </div>

    <a-card class="operation-card">
      <div class="operation-bar">
        <a-space>
          <a-date-picker v-model:value="params.date" valueFormat="YYYY-MM-DD" placeholder="请选择日期" />
          <a-button type="primary" @click="handleQuery()" class="refresh-btn">
            <template #icon><search-outlined /></template>
            查询车票
          </a-button>
        </a-space>
        <div class="statistics">
          <div class="stat-item">
            <dashboard-outlined />
            <span>车票总数: {{ pagination.total }}</span>
          </div>
        </div>
      </div>
    </a-card>

    <a-card class="table-card">
      <a-table
        :dataSource="tickets"
        :columns="columns"
        :pagination="pagination"
        @change="handleTableChange"
        :loading="loading"
        :locale="{ emptyText: '暂无车票数据' }"
        rowKey="id"
        bordered
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'operation'">
            <a-space>
              <a-popconfirm
                  title="退票后不可恢复，确认删除?"
                  @confirm="onDelete(record)"
                  ok-text="确认"
                  cancel-text="取消"
              >
                <a class="action-link delete-link">
                  <i class="fas fa-trash-alt"></i> 退票
                </a>
              </a-popconfirm>
            </a-space>
          </template>
          <template v-if="column.dataIndex === 'col'">
            <a-tag :color="record.col === 'Y' ? 'green' : 'orange'">
              <span v-for="item in SEAT_COL_ARRAY" :key="item.code">
                <span v-if="item.code === record.col && item.type === record.seatType">{{item.desc}}</span>
              </span>
            </a-tag>
          </template>
          <template v-else-if="column.dataIndex === 'seatType'">
            <a-tag :color="record.seatType === 'Y' ? 'green' : 'orange'">
              <span v-for="item in SEAT_TYPE_ARRAY" :key="item.code">
                <span v-if="item.code === record.seatType">{{item.desc}}</span>
              </span>
            </a-tag>
          </template>
        </template>
      </a-table>
    </a-card>

  </div>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue';
import { notification } from "ant-design-vue";
import axios from "axios";
import {
   DashboardOutlined, SearchOutlined
} from '@ant-design/icons-vue';

export default defineComponent({
  name: "ticket-view",
  components: {
    SearchOutlined,
    
    DashboardOutlined
  },
  setup() {
    const SEAT_COL_ARRAY = window.SEAT_COL_ARRAY;
    const SEAT_TYPE_ARRAY = window.SEAT_TYPE_ARRAY;
    const visible = ref(false);
    let ticket = ref({
      id: undefined,
      memberId: undefined,
      passengerId: undefined,
      passengerName: undefined,
      date: undefined,
      trainCode: undefined,
      carriageIndex: undefined,
      row: undefined,
      col: undefined,
      start: undefined,
      startTime: undefined,
      end: undefined,
      endTime: undefined,
      seatType: undefined,
      createTime: undefined,
      updateTime: undefined
    });
    const tickets = ref([]);
    const pagination = ref({
      total: 0,
      current: 1,
      pageSize: 10,
    });
    let loading = ref(false);
    const columns = [
      {
        title: '乘客姓名',
        dataIndex: 'passengerName',
        key: 'passengerName',
      },
      {
        title: '日期',
        dataIndex: 'date',
        key: 'date',
      },
      {
        title: '车次编号',
        dataIndex: 'trainCode',
        key: 'trainCode',
      },
      {
        title: '箱序',
        dataIndex: 'carriageIndex',
        key: 'carriageIndex',
      },
      {
        title: '排号',
        dataIndex: 'row',
        key: 'row',
      },
      {
        title: '列号',
        dataIndex: 'col',
        key: 'col',
      },
      {
        title: '出发站',
        dataIndex: 'start',
        key: 'start',
      },
      {
        title: '出发时间',
        dataIndex: 'startTime',
        key: 'startTime',
      },
      {
        title: '到达站',
        dataIndex: 'end',
        key: 'end',
      },
      {
        title: '到站时间',
        dataIndex: 'endTime',
        key: 'endTime',
      },
      {
        title: '座位类型',
        dataIndex: 'seatType',
        key: 'seatType',
      },
      {
        title:'操作',
        dataIndex: 'operation'
      }
    ];

    let params=ref({
      date:null
    })

    const handleQuery = (param) => {
      if (!param) {
        param = {
          page: 1,
          size: pagination.value.pageSize
        };
      }
      loading.value = true;
      axios.get("/member/admin/ticket/query-list", {
        params: {
          page: param.page,
          size: param.size,
          date:params.value.date
        }
      }).then((response) => {
        loading.value = false;
        let data = response.data;
        if (data.success) {
          tickets.value = data.content.list;
          pagination.value.current = param.page;
          pagination.value.total = data.content.total;
        } else {
          notification.error({description: data.message});
        }
      }).catch(() => {
        loading.value = false;
      });
    };

    const onDelete = (record) => {
      axios.delete("/member/ticket/delete/" + record.id).then((response) => {
        const data = response.data;
        if (data.success) {
          notification.success({description: "退票成功！"});
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize,
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const handleTableChange = (pagination) => {
      handleQuery({
        page: pagination.current,
        size: pagination.pageSize
      });
    };

    onMounted(() => {
      handleQuery({
        page: 1,
        size: pagination.value.pageSize
      });
    });

    return {
      SEAT_COL_ARRAY,
      SEAT_TYPE_ARRAY,
      ticket,
      visible,
      tickets,
      pagination,
      columns,
      handleTableChange,
      handleQuery,
      loading,
      params,
      onDelete
    };
  },
});
</script>

<style scoped>
.ticket-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

.header {
  margin-bottom: 24px;
  text-align: center;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #1f2f3d;
  margin-bottom: 8px;
  background: linear-gradient(90deg, #1890ff, #722ed1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  color: #606266;
  font-size: 16px;
}

.operation-card {
  margin-bottom: 24px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.table-card {
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.operation-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.refresh-btn, .add-btn {
  border-radius: 6px;
  font-weight: 500;
  padding: 0 20px;
  height: 36px;
}

.add-btn {
  background: linear-gradient(90deg, #13c2c2, #1890ff);
  border: none;
}

.statistics {
  display: flex;
  gap: 20px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f0f9ff;
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 500;
  color: #1890ff;
}

.operation-buttons {
  display: flex;
  justify-content: center;
}

.edit-btn {
  background: linear-gradient(90deg, #52c41a, #389e0d);
  border: none;
}

.delete-btn {
  border: none;
}

:deep(.ant-table-thead > tr > th) {
  background-color: #f0f9ff !important;
  font-weight: 600;
  color: #1f2f3d;
}

:deep(.ant-table-row) {
  transition: background-color 0.3s;
}

:deep(.ant-table-row:hover) {
  background-color: #f0f9ff !important;
}

.ticket-modal :deep(.ant-modal-header) {
  border-radius: 10px 10px 0 0;
}

.ticket-modal :deep(.ant-modal-body) {
  padding: 24px;
}

.ticket-modal :deep(.ant-form-item) {
  margin-bottom: 20px;
}

.ticket-modal :deep(.ant-input) {
  border-radius: 6px;
  height: 40px;
}

@media (max-width: 768px) {
  .operation-bar {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }

  .ticket-container {
    padding: 15px;
  }

  .title {
    font-size: 24px;
  }

  .statistics {
    width: 100%;
    justify-content: flex-start;
  }
}
</style>